/**
 * Created by Demon on 2015/6/15.
 */
var ChimneyPage = function () {
    var inputs = {
        E306: $("#E306"),
        E307: $("#E307"),
        E308: $("#E308"),
        E309: $("#E309"),
        E314: $("#E314"),
        E315: $("#E315"),
        E316: $("#E316"),
        E318: $("#E318"),
        E321: $("#E321"),
        E322: $("#E322"),
        E323: $("#E323"),
        E324: $("#E324"),
        E325: $("#E325"),
        E326: $("#E326"),
        E327: $("#E327"),
        E328: $("#E328")
    };

    var selects = {
        E320: $("#E320_select"),
        E321: $("#E321_select")
    };

    function selectHandle() {
        selects.E321.on("change", function () {
            inputs.E321.val($(this).val())
        });

        inputs.E321.val(selects.E321.val())
    }

    function popoverHandle() {

        var popoverMessage = {
            E322: {
                title: "提示信息",
                message: "直型筒坡度i = 0"
            }
        };

        inputs.E322.popover({
            title: popoverMessage.E322.title,
            content: popoverMessage.E322.message,
            trigger: "focus"
        })
    }

    function calculateInit() {
        $.ajax({
            url: basePath + "combustion/chimney/getCalculateInit",
            dataType: "json",
            type: "post"
        })
    }

    return {
        init: function () {
            selectHandle();
            popoverHandle();
            calculateInit();
            $("#verification").on("click", function () {
                $("#Y").hide();
                $("#N").hide();
                $.ajax({
                    url: basePath + "admin/formal/calculate",
                    dataType: "json",
                    type: "post",
                    //燃烧前缀 R
                    data: "paramName=RE329",
                    success: function (data) {
                        if (data > 0) {
                            $("#Y").show();
                            $("#modal").modal("show")
                        }
                        else {
                            $("#N").show();
                            $("#modal").modal("show")
                        }
                    },
                    error: function () {
                        $("#qualified").hide();
                        $("#disqualified").hide();
                    }
                });
            });
        },

        updata: function (ExParam) {
            //定义保存数据方法
            var save = function (exParam, exVal) {
                if (validate.isNotNumber(exVal)) {
                    validate.invalid();
                    return;
                }
                $.ajax({
                    url: basePath + "combustion/chimney/saveValueOfChimneyEx",
                    dataType: "json",
                    type: "post",
                    //放了三个值，一个填到默认Ex当中，其他两个分别对应属性值和属性名
                    data: exParam + "=" + exVal + "&paramName=" + exParam + "&paramValue=" + exVal
                });
                validate.valid();
            };
            var $obj = $("#" + ExParam);
            var ExVal = $obj.val();
            validate.init($obj);
            if (ExVal != "") {
                if ($obj.hasClass("nofill"))
                    return;
                save(ExParam, ExVal);
            } else {
                //判断是否可以计算，如果可以后台计算后返回结果，保存结果，不可以计算提示
                if ($obj.hasClass("calculable")) {
                    $.ajax({
                        url: basePath + "admin/formal/calculate",
                        dataType: "json",
                        type: "post",
                        //燃烧前缀 R
                        data: "paramName=R" + ExParam,
                        success: function (data) {
                            $obj.val(data);
                            save(ExParam, data);
                            validate.valid();
                        },
                        error: function () {
                            validate.invalid();
                        }
                    });
                }
                else
                    validate.invalid();
            }
        },
        calcul:function(ExParam) {
            Metronic.blockUI({
                boxed:true,
                message:"数据计算中..."
            });
            var save = function (exParam, exVal) {
                if (validate.isNotNumber(exVal)) {
                    validate.invalid();
                    return;
                }
                $.ajax({
                    url: basePath + "combustion/chimney/saveValueOfChimneyEx",
                    dataType: "json",
                    type: "post",
                    data: exParam + "=" + exVal + "&paramName=" + exParam + "&paramValue=" + exVal
                });
                validate.valid();
            };

            var $obj = $("#" + ExParam);
            $.ajax({
                url: basePath + "admin/formal/calculate",
                dataType: "json",
                type: "post",
                data: "paramName=R" + ExParam,
                success: function (data) {
                    $obj.val(data);
                    Metronic.unblockUI();
                    save(ExParam, data);
                }
            });
        }
    }
}();
